﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Room_Management
{
    public partial class fConfig : Form
    {
        private OleDbConnection myconn;
        private OleDbCommand cmd;
        int temp;
        string cbSelectItem;
        public fConfig()
        {
            cmd = new OleDbCommand();
            //myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""E:\Fpt University\C#\LearnCsharp\Room Management\Database\RoomManagement.mdb"";Persist Security Info=false");
            //this.myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\Users\claM\Desktop\LearningC#\Room Management\Database\RoomManagement.mdb"";Persist Security Info=false");
            myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""Database\RoomManagement.mdb"";Persist Security Info=false");
            cmd.Connection = myconn;
            this.myconn.Open();

            InitializeComponent();
            
            // Add room information into combobox
            ComboBoxAddValue();
        }

        private void cbChangePRoom_CheckedChanged(object sender, EventArgs e)
        {
            if (cbChangePRoom.Checked)
            {
                this.cbRoom.Enabled = true;
                this.tbPriceRoom.Enabled = true;
                this.dtpStartDRoom.Enabled = true;
            }
            else
            {
                this.cbRoom.Enabled = false;
                this.tbPriceRoom.Enabled = false;
                this.dtpStartDRoom.Enabled = false;
            }
        }
        
        private void cbChangePElectric_CheckedChanged(object sender, EventArgs e)
        {
            if (cbChangePElectric.Checked)
            {
                this.tbChangeElectric.Enabled = true;
                this.dtpChangeDElectric.Enabled = true;
            }
            else
            {
                this.tbChangeElectric.Enabled = false;
                this.dtpChangeDElectric.Enabled = false;
            }
        }

        private void cbChangePWater_CheckedChanged(object sender, EventArgs e)
        {
            if (cbChangePWater.Checked)
            {
                this.tbChangePWater.Enabled = true;
                this.dtpChangeDWater.Enabled = true;
            }
            else
            {
                this.tbChangePWater.Enabled = false;
                this.dtpChangeDWater.Enabled = false;
            }
        }

        private void cbAddNewRoom_CheckedChanged(object sender, EventArgs e)
        {
            if (cbAddNewRoom.Checked)
            {
                this.tbRoomName.Enabled = true;
                this.tbNRPrice.Enabled = true;
                this.dtpNRoom.Enabled = true;
            }
            else
            {
                this.tbRoomName.Enabled = false;
                this.tbNRPrice.Enabled = false;
                this.dtpNRoom.Enabled = false;
            }
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            if (cbChangePRoom.Checked) 
            {
                if (cbRoom.Text == "Chọn Phòng:")
                {
                    bSave.Enabled = false;
                    MessageBox.Show("Hãy chọn phòng bạn muốn thanh toán!");
                }
                else 
                {
                    cbSelectItem = cbRoom.Text;

                    //MessageBox.Show("SelectText :" + cbSelectItem.ToString());
                    cmd = new OleDbCommand();
                    cmd.Connection = myconn;
                    cmd.CommandText = "SELECT ID FROM Room WHERE RoomName ='" + cbSelectItem + "'";
                    string strRoomid = cmd.ExecuteScalar().ToString();

                    cmd = new OleDbCommand();
                    cmd.Connection = myconn;
                    cmd.CommandText = "INSERT INTO [Room Fee](RoomID,Fee,StartFromDate) VALUES (" + Convert.ToInt32(strRoomid) + ",'" + this.tbPriceRoom.Text + "','" + this.dtpStartDRoom.Value + "')";
                    temp = cmd.ExecuteNonQuery();
                    if (temp > 0)
                    {
                        MessageBox.Show("Chỉnh giá " + cbSelectItem.ToLower() + " thành công!");
                    }
                    else
                    {
                        MessageBox.Show("Chỉnh giá " + cbSelectItem.ToLower() + " thất bại!");
                    }
                }
            }

            if (cbChangePElectric.Checked) 
            {
                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "INSERT INTO [Electric Average](AverElectric,StartFromDate) VALUES ('"+ this.tbChangeElectric.Text +"','"+ this.dtpChangeDElectric.Value +"')";

                temp = cmd.ExecuteNonQuery();
                if (temp > 0)
                {
                    MessageBox.Show("Chỉnh giá điện thành công!");
                }
                else
                {
                    MessageBox.Show("Chỉnh giá điện thất bại");
                }
            }
            
            if (cbChangePWater.Checked) 
            {
                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "INSERT INTO [Water Average](AverWater,StartFromDate) VALUES ('" + this.tbChangePWater.Text + "','" + this.dtpChangeDWater.Value + "')";

                temp = cmd.ExecuteNonQuery();
                if (temp > 0)
                {
                    MessageBox.Show("Chỉnh giá nước thành công!");
                }
                else
                {
                    MessageBox.Show("Chỉnh giá nước thất bại!");
                }
            }

            if (cbAddNewRoom.Checked) 
            {
                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "INSERT INTO [Room](RoomName,Status) VALUES ('" + this.tbRoomName.Text + "','Trống')";
                cmd.ExecuteNonQuery();

                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "SELECT ID FROM Room WHERE RoomName ='" + this.tbRoomName.Text + "'";
                string strRoomid = cmd.ExecuteScalar().ToString();

                cmd = new OleDbCommand();
                cmd.Connection = myconn;
                cmd.CommandText = "INSERT INTO [Room Fee](RoomID,Fee,StartFromDate) VALUES (" + Convert.ToInt32(strRoomid) + ",'" + this.tbNRPrice.Text + "','" + this.dtpNRoom.Value + "')";

                temp = cmd.ExecuteNonQuery();
                if (temp > 0)
                {
                    MessageBox.Show("Thêm " + this.tbRoomName.Text.ToLower() + " thành công!");
                }
                else
                {
                    MessageBox.Show("Thêm " + this.tbRoomName.Text.ToLower() + " thất bại!");
                }
            }
        }

        private void ComboBoxAddValue()
        {
            cmd.CommandText = "SELECT * FROM Room";
            OleDbDataReader drRoom = cmd.ExecuteReader();
            ArrayList Rooms = new ArrayList();
            Rooms.Add(new AddValue("Chọn Phòng:", 0));
            while (drRoom.Read())
            {
                Rooms.Add(new AddValue(drRoom.GetString(1), drRoom.GetInt32(0)));
            }
            drRoom.Close();

            this.cbRoom.DataSource = Rooms;
            this.cbRoom.DisplayMember = "Display";
            this.cbRoom.ValueMember = "Value";
        }

        public class AddValue
        {
            private string m_Display;
            private long m_Value;
            public AddValue(string Display, long Value)
            {
                m_Display = Display;
                m_Value = Value;
            }
            public string Display
            {
                get { return m_Display; }
            }
            public long Value
            {
                get { return m_Value; }
            }
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            fMInterface parent = new fMInterface();
            parent.Show();
        }

        private void cbRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRoom.Text == "Chọn Phòng:")
            {
                bSave.Enabled = false;
                MessageBox.Show("Hãy chọn phòng bạn muốn thanh toán!");
            }
            else
            {
                bSave.Enabled = true;
            }
        }
    }
}

